// pages/login/index.js
const app = getApp();
import Notify from '@vant/weapp/notify/notify'
import {getPhoneCity,getCode} from "../../utils/request/Api"
let storedTimestamp = wx.getStorageSync('sendTimestamp');
const indexList = [];
const charCodeOfA = 'A'.charCodeAt(0);
let timer;
for (let i = 0; i < 26; i++) {
  indexList.push(String.fromCharCode(charCodeOfA + i));
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false, //电话选择
    title: "+86", //电话前缀
    value: "", //电话
    theme: '', //主题颜色
    windowHeight: 0, //手机屏幕高度
    phoneInputHeight: 60, //logo高
    user: "", //用户名
    pass: "", //密码
    isPass: true, //账号登录显示
    centerHeight: 20,
    bottonHeight: 10,
    indexList,
    scroll_view: "", //点击字母定位
    char_view: "A", //滚动屏幕相对应字母高光
    char_height: 0, //字母高度
    countryList:[],
    flag:false
   
  },
  showPopup() {
    this.setData({
      show: true
    });
  },
  onClose() {
    this.setData({
      show: false
    });
  },
  onChange(e) {
    const {detail} = e
    this.setData({
      value:detail
    })
  },
 
  onLogin() {
    const {
      value,
      user,
      pass,
      isPass
    } = this.data
    if (isPass && value == "") {
      Notify('请填写手机号');
      return;
    }

    if ((user == "" || pass == "") && isPass!=true) {
      Notify('请填写账号密码');
      return;
    }
    
    if(value!=""){
      // wx.setStorageSync('sendTimestamp', Date.now());
      const data = {
        user_phone :value
      }
     
      getCode(data).then(res=>{
        if(res.code==200){
          Notify(res.message);
          wx.navigateTo({
            url: '../code/index?phone='+value,
          })
          return
        }
        Notify(res.message);
      })
     
    }

  },
  onFocus(e) {
    const {
      windowHeight
    } = this.data
    const inputHeight = e.detail.height
    this.setData({
      phoneInputHeight: (inputHeight - 30) / (windowHeight / 100),
      inputshow: false
    })

  },
  onBlur(e) {
    this.setData({
      phoneInputHeight: 60,
      inputshow: true
    })
  },
  onSwitch(e) {
    const {
      isPass,
    } = this.data
    if (isPass) {
      this.setData({
        centerHeight: 30,
        phoneInputHeight: 20,
        bottonHeight: 15
      })
    } else {
      this.setData({
        centerHeight: 20,
        phoneInputHeight: 60,
        bottonHeight: 10
      })
    }
    this.setData({
      isPass: !isPass,
      value :""
    })
  },

 
     selectcity(e) {
      const {
        currentTarget: {
          dataset: {
            item
          }
        }
      } = e
      
      this.setData({
        scroll_view: item,
        char_view: item,
        char_height:0
      })
    
    },
    bindscroll(e) {
      this.getRect()
    },
    handScrollEnd(e) {
      const {
        data: {
          char_height,
          indexList,
          char_view
        }
      } = this
      const {
        detail: {
          scrollTop
        }
      } = e
      if (char_height) {

        const index = (scrollTop / char_height).toFixed();
        const height = char_height * (indexList.length - 1)

        const string = indexList[index];
        
        if (string != char_view && scrollTop <= height &&char_height!=0) {
          this.setData({
            char_view: string
          })
        }

      }
    },
    getRect() {
      const that = this
      if (this.data.char_height == 0) {
        this.createSelectorQuery().select('.brand-item').boundingClientRect(function (rect) {
          that.setData({
            char_height: rect.height
          })
        }).exec()
      }
    },
  onClickNumber(e){
    
    const {code,id} = e.currentTarget.dataset
    this.setData({
      title:"+"+code,
      show:false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      theme: app.globalData.theme,
      windowHeight: app.globalData.windowHeight

    });
    getPhoneCity().then(res=>{
      if(res.code==200){
        this.setData({
          countryList:res.data.list
        })
      }
    })
  
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (storedTimestamp) {
      let remainingTime = Math.ceil((storedTimestamp + 60000 - Date.now()) / 1000);
      timer= setInterval(()=>{
        if (remainingTime > 0) {
          // 显示剩余时间或保持按钮禁用状态
          this.setData({
            flag :true
          })
        remainingTime--;
          Notify('请'+remainingTime+"秒之后重新登录");
        }else{
          this.setData({
            flag :false
          })
          wx.removeStorageSync('sendTimestamp');
          clearInterval(timer)
          timer=null
        }
      },1000)
     
    
        // 清除存储的时间戳，允许重新发送
      
      
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})